package com.bipa.service;

import com.bipa.bean.Article;
import com.bipa.bean.ShopCar;
import com.bipa.dao.ArticleDao;
import com.bipa.dao.ShopCarDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ShopCarService {

    @Autowired
    ShopCarDao shopCarDao;

    @Autowired
    ArticleDao articleDao;

    public boolean getShopCarByUserIdAndArticleId(int userId,int articleId){
        return shopCarDao.getShopCarByUserIdAndArticleId(userId,articleId) != null;
    }

    public void updateShopcar(int userId, int articleId, int number){
        shopCarDao.updateShopcar(userId,articleId,number);
    }

    public void updateShopcarTow(int userId, int articleId, int number){
        shopCarDao.updateShopcarTow(userId,articleId,number);
    }

    public void addShopcar(int userId, int articleId, int number){
        shopCarDao.addShopcar(userId,articleId,number);
    }

    public void deleteShopCarById(int userId, int articleId){
        shopCarDao.deleteShopCarById(userId,articleId);
    }

    public List<ShopCar> getShopCarByUserId(int id){
        List<ShopCar> shopCars = shopCarDao.getShopCarByUserId(id);
        for(ShopCar shopCar : shopCars) {
            //根据商品id获取商品信息
            Article article = articleDao.getArticleById(shopCar.getArticle_id());
            shopCar.setArticle(article);
        }
        return shopCars;
    }
}
